﻿using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using S3.Common;

namespace S3
{
    public delegate void InitializeComponentHandler();
    internal delegate void SetControlValueCallback(Control control, string propName, object propValue);

    public partial class PopupProgress : XtraForm
    {
        public PopupProgress()
        {
            InitializeComponent();
        }

        public UserAction Action { get; set; }

        public event InitializeComponentHandler OnInitializeComponentHandler;

        private void SetControlPropertyValue(Control control, string propName, object propValue)
        {
            if (control.InvokeRequired)
            {
                SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                control.Invoke(d, new object[] {control, propName, propValue});
            }

            else
            {
                Type t = control.GetType();
                PropertyInfo prop =
                    t.GetProperties().Where(pro => pro.Name.ToLower() == propName.ToLower()).FirstOrDefault();
                if (prop != null)
                {
                    prop.SetValue(control, propValue, null);
                }
            }
        }
     
        private S3FormBase _form;

        public S3FormBase FormOwner
        {
            get { return _form; }
            set
            {
                _form = value;
                _form.OnLoadData += new RunLoadDataProcessHandler(Form_OnRunLoading);
            }
        }

        private void Form_OnRunLoading()
        {
            FormOwner.DoLoadData();
        }

        private void PopupProgressShown(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            SetControlPropertyValue(lblLoading, "Text", this.Text);
            if (OnInitializeComponentHandler != null)
            {
                OnInitializeComponentHandler();
                Thread.Sleep(100);
            }
            SetControlPropertyValue(lblLoading, "Text", this.Text);
            switch (Action)
            {
                case UserAction.Load:
                    this.FormOwner.RunLoadData();
                    break;
                case UserAction.Save:
                    this.FormOwner.RunSaveData();
                    break;
                case UserAction.Update:
                    this.FormOwner.RunUpdateData();
                    break;
                case UserAction.Delete:
                    this.FormOwner.RunDetelteData();
                    break;
            }
        }

        private void RunWorkerCompleted(object sender,
                                        System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }
    }
}